<?php
namespace app\personal\controller;

use think\Controller;
use think\Request;
use think\Db;

class Article extends Controller
{
    /**
     *   初始化
     *  判断是否登陆
     */
    public function _initialize()
    {
        if (session('tel') == null){
            return $this->error('您还未登陆,请先登陆!');
        }
    }

    /**
     * 模块页面
     * 显示一级列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $liste = Db::table('article')->field('amage,aname,regtime')->where('dispaly = 1 and uid ='.session('uid'))->select();
        $list = Db::table('column')->field('cid,cname')->where('pid = 0')->select();
        $ass = Db::name('subscribe s')
            ->join('column c','s.cid = c.cid')
            ->where('s.uid ='.session('uid'))
            ->limit(9)
            ->select();
        $user = Db::table('user_self')->field('nickname,icon')->where('uid ='.session('uid'))->find();
        return view('Article/index', [
            'title' => '【众评网】为你私人定制的资讯客户端 --- 【che-cn.com】 - 个人中心',
            'list' => $list,
            'liste' => $liste,
            'ass' => $ass,
            'user' => $user,
        ]);
    }

    /**
     * 显示二级列表.
     *
     * @return \think\Response
     */
    public function create($id)
    {
        $lister = Db::table('column')->field('cid,cname')->where('pid =' . $id)->select();
        return json($lister);
    }

    /**
     * 上传文章到数据库
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function demo()
    {
        $file = request()->file('mage');
        $arr = request()->post();

        $rst = $this->validate(
            [
                'mage'=>$file,
                'content'=>$arr['content'],
                'name'=>$arr['aname'],
            ],
            [
                ['mage','require|image','文章封面图不能为空!|请上传图片!'],
                ['content','require','文章内容不能为空!'],
                ['name','require|length:4,20','不能为空|只能输入4~20位'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }

        $info = $file->move(ROOT_PATH . 'public/static' . DS . 'uploads');
        if($info){
            $imgSrc='static/uploads/'.str_replace('\\',"/",$info->getSaveName());
            $image = \think\Image::open($imgSrc);
            $image->thumb(664, 300,6)->save($imgSrc);//生成缩略图、删除原图以及添加水印
            // 成功上传后 获取上传信息
            $info = 'static/uploads/'.str_replace('\\',"/",$info->getSaveName());
        }else{
            // 上传失败获取错误信息
            return $this->success('上传失败');
        }
        $data = ['uid'=>session('uid'),'aname'=>$arr['aname'],'cid'=>$arr['cid'],'regtime'=>time(),'amage'=>$info];
        $result = Db::table('article')->insertGetId($data);
        if (!$result > 0){
            return $this->success('添加失败');
        } else {
            $content = ['aid'=>$result,'content'=>$arr['content']];
            $res = Db::table('article_func')->insert($content);
            return $this->success('添加成功', url('/personal'));
        }
    }

    /**
     * 文章图片上传
     */
    public function upload()
    {

        $file = request()->file('imgFile');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public/static' . DS . 'uploads');
//        $info = 'uploads/'.str_replace('\\',"/",$info->getSaveName());
        if($info){
            $imgSrc='static/uploads/'.str_replace('\\',"/",$info->getSaveName());
            $image = \think\Image::open($imgSrc);
            $image->thumb(650, 450,6)->save($imgSrc);//生成缩略图、删除原图以及添加水印
            // 成功上传后 获取上传信息
            $info = '/static/uploads/'.str_replace('\\',"/",$info->getSaveName());
//            echo $info;
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }

        if ($info == false) {
            return json(array('error' => 1, 'massage' => '上传失败'));
        }
        $result = [
            "error" => 0,
        "url" => $info];
        return json($result);
    }



}
